package org.zhouhe.modules.hotinfo.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.zhouhe.base.BaseService;
import org.zhouhe.page.PageUtil;
import org.zhouhe.vo.HotInfo;
import org.zhouhe.vo.HotInfoType;

@Service
@SuppressWarnings("unchecked")
public class HotInfoService extends BaseService {
	public HotInfo getHotInfoDetail(HotInfo hotInfo) {
		return (HotInfo) dao.get(HotInfo.class, hotInfo.getHotInfoId());
	}

	public PageUtil getHotInfoList(PageUtil pageUtil, HotInfo hotInfo) {
		if (pageUtil == null) {
			pageUtil = new PageUtil();
		}
		List<Object> paramList = new ArrayList<Object>();
		StringBuilder from = new StringBuilder();
		from.append("from HotInfo where 1=1");
		if (hotInfo == null) {
			hotInfo = new HotInfo();
		}
		// 文章类型：
		if (hotInfo.getHotInfoType() != null && hotInfo.getHotInfoType().getHotInfoTypeId() > 0) {
			from.append(" and hotInfoType.hotInfoTypeId = ? ");
			paramList.add(hotInfo.getHotInfoType().getHotInfoTypeId());
		}
		pageUtil.setHQL("", from.toString(), paramList);
		return dao.findByPage(pageUtil);
	}

	public List<HotInfoType> getHotInfoTypeList() {
		return dao.find("from HotInfoType where status=1 order by hotInfoTypeId asc");
	}

	public List<HotInfo> getHotInfoByType(Integer typeId, Integer count) {
		List<HotInfo> hotInfoList = null;
		String hql = "from HotInfo where status = 1";
		if(typeId != null){
			hql += " and hotInfoType.hotInfoTypeId = "+ typeId ;
		}
		hotInfoList = (List<HotInfo>) dao.getSessionFactory().getCurrentSession().createQuery(hql).setMaxResults(count).list();
		return hotInfoList;
	}

	public void save(HotInfo hotInfo) {
		hotInfo.setStatus(1);
		hotInfo.setCreateTime(new Timestamp(new Date().getTime()));
		dao.save(hotInfo);
	}

	public void update(HotInfo hotInfo) {
		HotInfo hotInfoBean = (HotInfo) dao.get(HotInfo.class, hotInfo.getHotInfoId());
		hotInfoBean.setHotInfoType(hotInfo.getHotInfoType());
		hotInfoBean.setTitle(hotInfo.getTitle());
		hotInfoBean.setContent(hotInfo.getContent());
		hotInfoBean.setDisplayTime(hotInfo.getDisplayTime());
		dao.update(hotInfoBean);
	}

	public void delete(HotInfo hotInfo) {
		dao.delete(hotInfo);
	}
}
